#include "sqlexe.h"
#include <iostream>
#include <QSqlError>
#include<QSqlQuery>


sqlexe::sqlexe()
{
  OpenDataBase();


}
sqlexe::~sqlexe()
{
    if(query!=NULL)
    {
        delete query;
    }
}

bool sqlexe::OpenDataBase()
{
     db=QSqlDatabase::addDatabase("QODBC");
     std::cout<<"ODBC driver?"<<db.isValid()<<std::endl;
     QString dsn=dsnname;
     db.setHostName("***");
     db.setDatabaseName(dsn);
     db.setUserName("sa");
     db.setPassword("***");

     if(!db.open())
     {
         QSqlError err;
         err=db.lastError();
         std::cout<<"DataBase error"<<err.text().toStdString()<<std::endl;
         return false;
     }
     else
     {
         std::cout<<"database open success!"<<std::endl;
         isOpen=true;

     }

     //test(sucessful)
     /*
     QSqlQuery query(db);
     query.exec("select * from student");
     QString msg;
     while(query.next())
     {
         msg+=query.value(0).toString()+query.value(1).toString()+query.value(2).toString()+query.value(3).toString();
     }
     std::cout<<msg.toStdString()<<std::endl;
     */

    return true;

}

bool sqlexe::exesql(QString sql)
{
    query=new QSqlQuery(db);
    query->exec(sql);

}

QSqlQuery sqlexe::getquery()
{
    return *query;
}
